package com.sl.au.controller;

import com.sl.au.entity.EventRegister;
import com.sl.au.entity.Option;
import com.sl.au.entity.PartBaseInfo;
import com.sl.au.entity.User;
import com.sl.au.entity.ViewForEventRegisterTask;
import com.sl.au.service.SectionService;
import com.sl.au.service.UserService;
import com.sl.au.service.UserTrajectoryService;
import com.sl.utils.ExdeUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.service.AttachmentService;
import com.sl.au.service.BusinesssellerAuthenticationService;
import com.sl.au.service.BusinesssellerBaseInfoService;
import com.sl.au.service.BussinessCheckService;
import com.sl.au.service.BussinessInspectionService;
import com.sl.au.service.EvaluateService;
import com.sl.au.service.EventRegisterService;
import com.sl.au.service.EventReportService;
import com.sl.au.service.GridAreaInfoService;
import com.sl.au.service.GridUserDetailsService;
import com.sl.au.service.IndexService;
import com.sl.au.service.OptionService;
import com.sl.au.service.PartBaseInfoService;
import com.sl.au.service.PartService;
import com.sl.au.service.HierarchyService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/ui/partShow")
public class PartShowController extends BaseController {

	@Autowired
	SectionService sectionService;
	@Autowired
	AttachmentService attachmentService;
	@Autowired
	IndexService indexService;
	@Autowired
	UserService userService;
	@Autowired
	PartService partService;
	@Autowired
	PartBaseInfoService partBaseInfoService;
	@Autowired
	GridAreaInfoService gridAreaInfoService;
	@Autowired
	GridUserDetailsService gridUserDetailsService;
	@Autowired
	EventReportService eventReportService;
	@Autowired
	OptionService optionService;
	@Autowired
	BusinesssellerAuthenticationService businesssellerAuthenticationService;
	@Autowired
	EvaluateService evaluateService;
	@Autowired
	BussinessInspectionService bussinessInspectionService;
	@Autowired
	BussinessCheckService bussinessCheckService;
	@Autowired
	BusinesssellerBaseInfoService businesssellerBaseInfoService;
	@Autowired
	HierarchyService hierarchyService;
	@Autowired
	UserTrajectoryService userTrajectoryService;

	@Autowired
	EventRegisterService eventRegisterService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = createLayoutView("partShow/partShowIndex2");
		return modelAndView;
	}
	
	@RequestMapping(value="getPartList", method = RequestMethod.GET)
	public Map<String,Object> getPartList() {
		Map<String,Object> map = new HashMap<String,Object>();
		List<PartBaseInfo> parts = partBaseInfoService.getRepository().getAllPart();
		map.put("parts", parts);
		return map;
	}


	@RequestMapping(value = "getPartInfo/{partId}", method = RequestMethod.GET)
	public Map<String, Object> getPartInfo(@PathVariable("partId") String partId) {
		Map<String, Object> map = new HashMap<String, Object>();
		PartBaseInfo part = partBaseInfoService.getRepository().findOne(partId);
		map.put("part", part);
		return map;
	}
	
	@RequestMapping(value = "getPartRealInfo/{partId}", method = RequestMethod.GET)
	public Map<String, Object> getPartRealInfo(@PathVariable("partId") String partId) {
		Map<String, Object> map = new HashMap<String, Object>();
		PartBaseInfo part = partBaseInfoService.getRepository().findOne(partId);
		String deviceIdentify = part.getDeviceIdentify();
		indexService.getParts(deviceIdentify, map);
		return map;
	}

	@RequestMapping(value = "refreashPartStatus", method = RequestMethod.GET)
	public Map<String, Object> refreashPartStatus() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<PartBaseInfo> parts = partBaseInfoService.getRepository().getAllPart();
		List<PartBaseInfo> warnings = new ArrayList<PartBaseInfo>();
		for (PartBaseInfo part : parts) {
			String deviceIdentify = part.getDeviceIdentify();
			Map<String, Object> result = new HashMap<String, Object>();
			indexService.getParts(deviceIdentify, result);
			if (result.containsKey("count")) {
				int count = ExdeUtil.str2int(String.valueOf(result.get("count")));
				for (int i = 0; i < count; i++) {
					boolean warning = (boolean) result.get("warning" + i);
					if (warning) {
						warnings.add(part);
						break;
					}
				}
			}
		}
		map.put("warnings", warnings);
		return map;
	}
}
